package cn.Liujialin.org.controller;

import cn.Liujialin.basic.util.AjaxResult;
import cn.Liujialin.basic.util.PageList;
import cn.Liujialin.org.domain.DictionaryDetail;
import cn.Liujialin.org.query.DictionaryDetailQuery;
import cn.Liujialin.org.service.IDictionaryDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/detail")
public class DictionaryDetailController {
    @Autowired
    private IDictionaryDetailService detailService;

    //添加和修改
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody DictionaryDetail dictionaryDetail) {
        try {
            if (dictionaryDetail.getId() == null) {
                detailService.save(dictionaryDetail);
            } else {
                detailService.update(dictionaryDetail);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.fail("操作失败");
        }
    }

    /**
     * 接口三要素：
     * 1.请求地址是什么 http://localhost:8080/dept/1  delete
     * 2.请求参数是什么 /{id}
     * 3.返回值是什么    AjaxResult
     * @param id
     * @return
     */
    //删除
    @DeleteMapping("/{id}")
    public AjaxResult deleteById(@PathVariable("id") Long id) {
        try {

            detailService.del(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.fail("删除失败");
        }
    }
    //获取一条数据
    @GetMapping("/{id}")
    public DictionaryDetail queryById(@PathVariable("id") Long id) {
        return detailService.findById(id);

    }

    //查询所有
    @GetMapping
    public List<DictionaryDetail> queryAll() {
        return detailService.findByAll();
    }

    //分页查询和高级查询
    @PostMapping
    public PageList<DictionaryDetail> queryPage(@RequestBody DictionaryDetailQuery query) {
        return detailService.queryPage(query);
    }

    //批量删除
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> ids) {
        try {
            detailService.patchDelete(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.fail("删除失败");
        }
    }
}
